@extends("layouts.backend.webadmin")
@section('page-header')
<h1>
    <small>ogz</small>
</h1>
@endsection

@section('content')
<link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2.min.css">
<link rel="stylesheet" href="{{env('APP_ROOT')}}/assets/select2/css/select2totree.css">
<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-xs-12">

            <div class="box">
                <!-- /.box-header -->
                <div class="box-body">
                    <table id="ogz-list" class="table table-bordered table-striped">
                        <thead>
                        <tr>

                        </tr>
                        </thead>

                        <tbody>
                        </tbody>
                    </table>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</section>
<form id="add-form">
    <section id="add" style="display: none;" class="content">
            <input type="hidden" name="id">
            <div class="form-group">
                <label for="parent-ogz">机构</label><br/>
                <select class="form-control" id="parent-ogz" name="ogz_id">
                    <!--<option value="0">--选择机构--</option>-->
                </select>
            </div>
        <div id="sort-order" style="display: none;" class="form-group">
            <label for="sort">排序</label>
            <input type="text" class="form-control" id="sort" placeholder="排序" name="sort">
        </div>
            <div class="form-group">
                <label for="menuname">名称</label>
                <input type="text" class="form-control" id="name" placeholder="名称" name="name">
            </div>


            <div class="form-group">
                <label for="description">描述</label>
                <input type="text" class="form-control" id="description" placeholder="描述" name="description">
            </div>

            <div class="form-group">
                <label for="parent-user">联系人</label><br/>
                <select style="width: 100%;" multiple="multiple" class="form-control user-multiple" id="parent-user" name="user_id[]">
                    <!--<option value="0">--选择联系人--</option>-->

                </select>
            </div>
            <button id="submit-data" type="button" class="btn btn-primary submit-data">确认</button>
    </section>
</form>
@permission('projectmanage-index')
<input type="hidden" name="pmsearch_access" value="1">
@endauth
<input type="hidden" name="ogzs" value="{{$ogzs}}">
<input type="hidden" name="users" value="{{$users}}">
@stop
@section('javascript-end')
<script src="{{env('APP_ROOT')}}/assets/layui/layui.all.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/select2.min.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/select2totree.js"></script>
<script src="{{env('APP_ROOT')}}/assets/select2/js/i18n/zh-CN.js"></script>
<script src="{{env('APP_ROOT')}}/js/common.js"></script>
<script>
    var m1 = manage();
    $(function () {
        //组织机构
        var ogzs = $.parseJSON($('input[name="ogzs"]').val());
        //联系人
        var users = $.parseJSON($('input[name="users"]').val());
        //多选联系人
        $('.user-multiple').select2();
        //添加
        $('.box-body').on('click','button.add-row',function(){
            //清空值
            $("#add-form")[0].reset();
            //清除隐藏域的值
            $(':input[type="hidden"]').val('');
            //切换按钮
            $('#submit-data2').attr('id','submit-data');
            //设置排序
            $('#sort-order').hide();
            //添加层
            addLayer = layer.open({
                title:'新增企业应用',
                zIndex:10,
                type:1,
                content:$("#add"),
                cancel:function(){
                    $('#add').hide();
                },
                success:function() {
                    //select ogz
                    $('#parent-ogz').html(m1.eachSelect(ogzs,'ogz',''));
                    //select tree
                    $('#parent-ogz').select2ToTree({language:'zh-CN'});
                    //select user
                    $('#parent-user').html(m1.eachSelect(users,'user',''));

                }
            });

        });
        $('#add-form').on('click','button#submit-data',function(){
            var fields = $("#add-form").serializeArray();
            $.ajax(
                {
                    url:"{{route('qyapp.add')}}",
                    method:"POST",
                    data:fields,
                    headers:{'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')}
                }
            ).done(function(data){
                if(data.code===0){
                    getToken();
                    //隐藏form
                    $("#add").hide();
                    layer.close(addLayer);
                    //刷新table跳转到首页
                    // tables.ajax.reload();
                    //刷新当前页
                    layer.msg('操作成功');
                    window.location.reload();
                } else {
                    layer.msg(data.msg);
                }
            }).fail(function (data) {
                $.each(data.responseJSON, function (key, value) {
                    var input = '#add-form input[name=' + key + ']';
                    // $(input).css("border","1px solid red");
                    layer.msg(value[0]);
                    return false;
                });

            });
        });
        //列表
        var tables = $('#ogz-list').DataTable({
            //调整结构
            "dom": '<"dataTables_filter">rtip',
            'oLanguage'   :{
                'sUrl'    : '{{env("APP_ROOT")}}/assets/bootstrap/js/zh_cn.json'
            },
            "paging": true, // 禁止分页
            "bSort": false,//禁止排序
            "processing": true,
            "initComplete": function( settings, json ) {
                var button = '<a href="{{route('qyapp.addpage')}}" class="btn btn-primary" style="float: left;">增加</a>';
                button += '<button class="btn btn-primary qyapp-export" style="float: left;margin-left: 5px;">导出</button>';
                $('.dataTables_filter').html(button);
                var _$this = this;

                m1.filter(jQuery,true,_$this,false);
            },
            //开启服务器模式
            serverSide: true,
            ajax: {
                //指定数据源
                url: "{{route('qyapp.search')}}",
                type: "GET",
                data: function(d){
                    delete d.columns;
                    return d;
                },
                //接受服务器数据
                dataSrc:function(json){
                    getToken();
                    return json.data;
                }
            },
            //每页显示三条数据
            pageLength: {{$pageNum}},
            //data值对应数据库字段名
            columns: [
                {
                    'title':'排序',
                    "data": "sort"
                },
                {
                    'title':'名称',
                    "data": "name",
                    "render": function(data,type,row,meta) {
                        var access =$('input[name="pmsearch_access"]').val();
                        if (access){
                            return '<a target="_blank" href="{{route('admin.projectmanage.index')}}?app='+row['id']+'">'+data+'</a>'
                        }else{
                            return data;
                        }
                    }
                },
                {
                    'title':'描述',
                    "data": "description"
                },
                {
                    'title':'机构',
                    "data": "ogz_name"
                },
                {
                    'title':'联系人',
                    "data": "user_name"

                },
                {
                    "title":"操作",
                    "targets": -2,
                    "data": null,
                    "render": function(data,type,row,meta){
                        var button = m1.button('qyapp');
                        if(row.deleted_at != null){
                            button1 = button[2];
                        }else{
                            button1 = button[1];
                        }
                        var edit = '<a href="{{route('qyapp.editpage')}}?id='+row['id']+'" class="btn btn-primary qyapp-update" type="button">编辑</a>';
                        return edit +"&nbsp"+ button1;
                    }
                }],
                //所有列过滤xss
                columnDefs: [{
                    "targets": "_all",
                    "render": $.fn.dataTable.render.text()
                }]
        });
        // 编辑
        $('#ogz-list tbody').on( 'click', 'button.editrow', function () {

            $('#sort-order').show();
            //更改添加按钮id
            $('#submit-data').attr('id','submit-data2');
            var data = tables.row($(this).parents('tr')).data();
            var fields = $("#add-form").serializeArray();

            jQuery.each( fields, function(i, field){
                //select
                $(":input[name='id']").val(data.id);
                $(":input[name='sort']").val(data.sort);
                var _name = $.trim(data.name);
                $(":input[name='name']").val(_name);

                $(":input[name='description']").val(data.description);
            });
            //显示编辑
            editLayer = layer.open({
                title:'修改企业应用',
                zIndex:10,
                type:1,
                content:$("#add"),
                end:function(){
                    // window.location.reload();
                    $('#add').hide();
                },
                success:function() {
                    //select ogz
                    $('#parent-ogz').html(m1.eachSelect(ogzs,'ogz',data.ogz_id));
                    //select tree
                    $('#parent-ogz').select2ToTree({language:'zh-CN'});
                    //select user
                    $('#parent-user').html(m1.eachSelect(users,'user',data.many_user));

                }
            });

        });
        //编辑提交
        $('#add-form').on('click','button#submit-data2',function(){
            fields = $("#add-form").serializeArray();
            $.ajax(
                {
                    url:"{{route('qyapp.update')}}",
                    method:"POST",
                    data:fields,
                    headers:{'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')}
                }
            ).fail(function(data){
                $.each(data.responseJSON, function (key, value) {
                    var input = '#add-form input[name=' + key + ']';
                    // $(input).css("border","1px solid red");
                    layer.msg(value[0]);
                    return false;
                });
            }).done(function(data){
                if(data.code===0){
                    getToken();
                    layer.msg('操作成功');
                    //关闭弹窗
                    layer.close(editLayer);
                    //刷新table跳转到首页
                    // tables.ajax.reload();
                    //刷新当前页
                    window.location.reload();
                }else{
                    layer.msg(data.msg);
                }
            });
        });
        //删除
        $('#ogz-list tbody').on( 'click', 'button.delrow', function () {
            // var _this = $(this);
            var data = tables.row($(this).parents('tr') ).data();
            m1.delete(jQuery,layer,"{{route('qyapp.delete')}}",data);

        });
        //启用
        $('#ogz-list tbody').on( 'click', 'button.activerow', function () {
            var data = tables.row($(this).parents('tr') ).data();
            m1.active(jQuery,layer,"{{route('qyapp.active')}}",data);
        });
        //导出
        $('.box-body').on('click','button.qyapp-export',function () {
            var exportUrl = "{{ URL::route('qyapp.export') }}";

            window.location.href = exportUrl;
        });
    });
</script>
@endsection